//
//  QYHoleListVC.swift
//  QingYuJSQ
//
//  Created by 李东 on 2024/4/6.
//

import UIKit
import SnapKit

class QYHoleListVC: QYBaseVC {
    private let CellID = NSStringFromClass(QYDataCell.self)
    
    lazy var dataList = [QYDataModel]()
    lazy var yearMonthLabel = UILabel()
    lazy var lastBtn = UIButton(type: .custom)
    lazy var nextBtn = UIButton(type: .custom)
    
    var currentMonth: Date!
    
    init(currentMonth: Date!) {
        super.init(nibName: nil, bundle: nil)
        self.currentMonth = currentMonth
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showPopBtn = true
        popBtn.setImage(UIImage(named: "arrow_back_white"), for: .normal)
        initUI()
        yearMonthLabel.text = df.string(from: currentMonth)
        dataList = QYDataHelper.shared.getLocalList().filter({ mm in
            mm.yearAndMonth == yearMonthLabel.text
        })
        tableView.reloadData()
    }
    
    func initUI()  {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(CGFloat.navHeight)
        }
        
        yearMonthLabel.textColor = .white
        yearMonthLabel.font = UIFont.boldSystemFont(ofSize: 16)
        yearMonthLabel.textAlignment = .center
        view.addSubview(yearMonthLabel)
        yearMonthLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(popBtn)
        }
        
        lastBtn.setImage(UIImage(named: "icon_last"), for: .normal)
        lastBtn.addTarget(self, action: #selector(lastHandle), for: .touchUpInside)
        view.addSubview(lastBtn)
        lastBtn.snp.makeConstraints { make in
            make.centerY.equalTo(yearMonthLabel)
            make.right.equalTo(yearMonthLabel.snp.left)
            make.size.width.height.equalTo(30)
        }
        
        nextBtn.setImage(UIImage(named: "icon_next"), for: .normal)
        nextBtn.addTarget(self, action: #selector(nextHandle), for: .touchUpInside)
        view.addSubview(nextBtn)
        nextBtn.snp.makeConstraints { make in
            make.centerY.equalTo(yearMonthLabel)
            make.left.equalTo(yearMonthLabel.snp.right)
            make.size.width.height.equalTo(30)
        }
    }
    
    lazy var df: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "yyyy年M月"
        return temp
    }()
    
    lazy var calendar: Calendar = {
        let temp = Calendar(identifier: .chinese)
        return temp
    }()
 

    lazy var tableView: UITableView = {
        let temp = UITableView(frame: .zero, style: .grouped)
        temp.backgroundColor = .init(hex: 0x3F6FE3)
        temp.separatorStyle = .none
        temp.delegate = self
        temp.dataSource = self
        temp.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: CGFloat.screenWidth, height: 20))
        temp.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: CGFloat.screenWidth, height: 0.01))
        temp.contentInsetAdjustmentBehavior = .never
        temp.register(UINib(nibName: "QYDataCell", bundle: nil), forCellReuseIdentifier: CellID)
        return temp
    }()
    
    //获取下个月的日期
    func nextMonth() -> Date {
        return calendar.date(byAdding: .month, value: 1, to: currentMonth) ?? currentMonth
    }
    
    //获取上个月的日期
    func lastMonth() -> Date {
        return calendar.date(byAdding: .month, value: -1, to: currentMonth) ?? currentMonth
    }
    
    @objc func nextHandle() {
        currentMonth = nextMonth()
        reloadData()
    }
    
    @objc func lastHandle()  {
        currentMonth = lastMonth()
        reloadData()
    }
    
    func reloadData() {
        yearMonthLabel.text = df.string(from: currentMonth)
        dataList = QYDataHelper.shared.getLocalList().filter({ mm in
            mm.yearAndMonth == yearMonthLabel.text ?? ""
        })
        tableView.reloadData()
    }
}
extension QYHoleListVC: UITableViewDelegate , UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        dataList.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(dataList[indexPath.section].getHeight)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CellID, for: indexPath) as! QYDataCell
        cell.model = dataList[indexPath.section]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        20
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        UIView()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        nil
    }
}
